package com.example.eduservice.controller;


import com.example.eduservice.entity.EduChapter;
import com.example.eduservice.entity.EduVideo;
import com.example.eduservice.entity.chapter.ChapterVo;
import com.example.eduservice.entity.vo.CourseInfoVo;
import com.example.eduservice.service.EduChapterService;
import com.example.eduservice.service.EduVideoService;
import com.example.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author xiuxian
 * @since 2022-06-26
 */
@RestController
@RequestMapping("/eduservice/chapter")
//@CrossOrigin    //跨域问题
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private EduVideoService eduVideoService;




    //课程大纲列表 根据id查询
    @GetMapping("/getChapterVideo/{courseId}")
    public  R getCharpterVideo(@PathVariable  String courseId){
        List<ChapterVo> list=chapterService.getChapterVideoByid(courseId);

        return R.ok().data("allChapter",list);
    }
    //添加章节
    @PostMapping("/addChapter")
    public  R  addChapter(@RequestBody EduChapter eduChapter){

        chapterService.save(eduChapter);
        return  R .ok();
    }

    //根据章节id查询
    @GetMapping("/getChapter/{chapterId}")
    public  R  getChapter(@PathVariable String chapterId){
        EduChapter eduChapter = chapterService.getById(chapterId);
        return  R.ok().data("chapter",eduChapter);
    }


    //修改章节
    @PostMapping("/updateChapter")
    public  R  updateChapter(@RequestBody EduChapter eduChapter){
         chapterService.updateById(eduChapter);
         return  R.ok();
    }

    //删除章节
    //删除章节的时候先删除全部小节的视频
    @DeleteMapping("/{chapterId}")
    public  R deleteChapter(@PathVariable String chapterId){

        EduChapter eduChapter = chapterService.getById(chapterId);

        //判断改课程id是否为空
        String courseId = eduChapter.getCourseId();
        if(!StringUtils.isEmpty(chapterId)){
            eduVideoService.removeVideoByid(courseId);
        }

        //删除章节
        boolean result = chapterService.deleteChapter(chapterId);
        if(result){
            return  R.ok();
        }else{
            return  R.error();
        }


    }
}

